﻿using Exam.infranstuctrue.Dto.Request.Arrange;
using Exam.infranstuctrue.Tools;
using Exam.Service.Interface;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ServiceStack.Redis;

namespace Exam.Controllers
{

    [ApiController]
    [Authorize]
    public class ArrangeController : BaseController
    {
        //RedisClient RedisClient = new RedisClient("127.0.0.1", 6379);
        private readonly IArrangeServer _arrangeServer;
        private readonly Session _session;

        public ArrangeController(IArrangeServer arrangeServer, Session session)
        {
            _arrangeServer = arrangeServer;
            _session = session;
        }
        /// <summary>
        /// 获取安排列表
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetArrange(int page = 1, int limit = 10, string? key = "")
        {
            var token = _session.GetToken();
            key = key ?? "";
            var result = await _arrangeServer.GetArrange(page, limit, key, token);
            return Ok(result);
        }
        /// <summary>
        /// 添加安排
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddArrange(ArrangeDto dto)
        {
            dto.acheckperson = _session.Uid;
            var result = await _arrangeServer.AddArrange(dto);
            return Ok(result);
        }
        /// <summary>
        /// 删除安排
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DeleteArrange(List<ArrangeDto> dto)
        {
            var result = await _arrangeServer.DeleteArrange(dto);
            return Ok(result);
        }

        /// <summary>
        /// 获取单个安排
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetEchoArrange(string aid)
        {
            var token = _session.GetToken();
            var result = await _arrangeServer.GetEchoArrange(aid, token);
            return Ok(result);
        }
        /// <summary>
        /// 修改安排
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateArrange(ArrangeDto dto)
        {
            var result = await _arrangeServer.UpdateArrange(dto);
            return Ok(result);
        }
        /// <summary>
        /// 阅卷
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ReviewTestpaper(ReviewTestpaperDto dto)
        {
            dto.tpuid=_session.Uid;
            var result = await _arrangeServer.ReviewTestpaper(dto);
            return Ok(result);
        }
    }
}
